<?php

require_once("Database/Model/ExerciseDAL.php");
//require_once("Database/Model/DatabaseConnection.php");

class WorkoutExercisesDAL
{
	/**
    *@var ExerciseDAL
    **/
	private $exerciseDAL;
	// private $databaseConnection;

	public function __construct()
	{
		$this->exerciseDAL = new ExerciseDAL();
		//$this->databaseConnection = new DatabaseConnection();
	}

	/**
    *@param int
    *@throws exeption
    **/
	public function DeleteWorkoutExercise($workoutID)
	{
		$sql = "DELETE FROM workoutexercises WHERE `WorkoutID` = $workoutID";  

		$mysqli = new mysqli("workoutdiary-185687.mysql.binero.se", "185687_ac16366", "password", "185687-workoutdiary", 3306);

		$stmt = $mysqli->prepare($sql);
	    if ($stmt == FALSE) 
	    {
	        throw new Exception("prepare of [$sql] failed " . $this->mysqli->error);
	    }

	    $result = $stmt->execute();
	    if ($result == FALSE) 
	    {
	        throw new Exception("execute of [$sql] failed " . $stmt->error);
	    }

	    $mysqli->close();
	}

	/**
    *@param int
    *@return array() Exerciselist
    **/
	public function getAllWorkoutExercisesList($workoutID)
	{	
       // $sql = "SELECT `e.ExerciseID`, e.MuscleGroupID, e.Name, e.Description 
		//		FROM workoutexercises as we 
	//				INNER JOIN exercise as e ON we.ExerciseID = e.ExerciseID 
	//			WHERE we.WorkoutID = $workoutID 
	//				ORDER BY e.MuscleGroupID";
    

		$sql = " SELECT e.ExerciseID, e.MuscleGroupID, e.Name, e.Description
				FROM workoutexercises AS we
				INNER JOIN exercise AS e ON we.ExerciseID = e.ExerciseID
				WHERE we.WorkoutID = $workoutID
				ORDER BY e.MuscleGroupID";

		return $this->exerciseDAL->ExerciseList($sql);
	}


	


//SELECT `e.ExerciseID`, e.MuscleGroupID, e.Name, e.Description 
//				FROM workoutexercises as we 
//					INNER JOIN exercise as e ON me.ExerciseID = e.ExerciseID 
//				WHERE we.WorkoutID = 27 



	/**
    *@param int
    *@param Exercise
    **/
	public function saveNewWorkoutExercise($workoutID, $ex) 
	{
		$mysqli = new mysqli("workoutdiary-185687.mysql.binero.se", "185687_ac16366", "password", "185687-workoutdiary", 3306); 

		$sql = "INSERT INTO workoutexercises
                        ( 
                                WorkoutID, 
                                ExerciseID
                        ) 
                        VALUES(?, ?)";

            $statement = $mysqli->prepare($sql);
            if ($statement === FALSE) 
            {
                     throw new Exception("prepare of $sql failed " . $mysqli->error); //kanske lägga i en errorMessage class
            }

            $ex = (int)$ex; // intVal($ex)
            if ($statement->bind_param("ii", $workoutID,$ex) === FALSE) 
            {
                    throw new Exception("bind_param of $sql failed " . $statement->error);
            }
            if ($statement->execute() === FALSE) 
            {
                    throw new Exception("execute of $sql failed " . $statement->error);
            }
		$mysqli->close();

	}
}